package cn.itcast.bos.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.user.IUserDao;
import cn.itcast.bos.domain.Role;
import cn.itcast.bos.domain.User;
import cn.itcast.bos.utils.MD5Utils;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

	@Resource
	private IUserDao userDao;
	public User login(User model) {
		//String password = MD5Utils.md5(model.getPassword());
		List<User> list = userDao.findByNamedQuery("findByUsernameAndPassword", model.getUsername(),model.getPassword());
		if (list != null && list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	@Override
	public void editPassword(String password, String id) {
		userDao.executeNamedQuery("editPassword", password,id);
	}

	//添加用户  同时关联角色
	public void save(User model, String[] roleIds) {
		userDao.save(model);
		if (roleIds != null && roleIds.length > 0) {
			for (String id : roleIds) {
				//用户关联角色
				Role role = new Role(id);
				model.getRoles().add(role);
			}
		}
	}
	@Override
	public List<User> findAll() {
		return userDao.findAll();
	}

}
